package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.POSSupplyView;

public class POSSupplyViewPresenter extends AbstractPresenter<POSSupplyView> {
	final String SINGLEITEM = "posSupplyView";
	final String MULTIITEM = "pOSSupplyViews";
	
	public ArrayList<POSSupplyView> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSSupplyView tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSSupplyView createItemFromJSONObject(JSONObject jsonSupply){
		POSSupplyView supply = new POSSupplyView();
		try{
			supply.setSupplyID(jsonSupply.getInt("supID"));
			supply.setSupplyName(jsonSupply.getString("supName"));
			supply.setResalePrice(jsonSupply.getDouble("supReSalePrice"));
			supply.setResaleable(jsonSupply.getBoolean("supResalable"));
			supply.setSupplyUnits(jsonSupply.getString("supUnits"));
			supply.setSupplyWearhouseID(jsonSupply.getInt("supWH"));
			supply.setTaxExempt(jsonSupply.getBoolean("taxExempt"));
			supply.setSupplyPrice(jsonSupply.getDouble("supPrice"));
			return supply;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSSupplyView item){
		try{
			JSONObject json = new JSONObject();
			json.put("supResalable", item.isResaleable());
			json.put("supReSalePrice", item.getResalePrice());
			json.put("supID", item.getSupplyID());
			json.put("supName", item.getSupplyName());
			json.put("supUnits", item.getSupplyUnits());
			json.put("supWH", item.getSupplyWearhouseID());
			json.put("taxExempt", item.isTaxExempt());
			json.put("supPrice", item.getSupplyPrice());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSSupplyView getEmptyEntity(){
		POSSupplyView entity = new POSSupplyView();
		return entity;
	}
}
